//
//  LYMusicTableViewCell.m
//  MusicMV
//
//  Created by Eternity on 16/3/17.
//  Copyright © 2016年 Eternity. All rights reserved.
//

#import "LYMusicTableViewCell.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
 
@interface LYMusicTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>


@property (nonatomic, retain) UIImageView *imgView; /**< 图片 */
@property (nonatomic, retain) UIImageView *smallImageView; /**< 头像图片 */
@property (nonatomic, retain) UIButton *titleButton; /**< 标题的button */
@property (nonatomic, retain) UILabel *autherLabel; /**< 作者名字的Label */
@property (nonatomic, retain) UIButton *singerButton; /**< 歌手的名字 */
@property (nonatomic, retain) UIImageView *clickImage; /**< 播放的图标 */
@property (nonatomic, retain) UICollectionView *bottonmCollectionView;
@property (nonatomic, retain) UISlider * timeSlider; /**< 进度条 */
@property (nonatomic, retain) AVAudioPlayer *avAudioPlayer;
@property (nonatomic, assign) BOOL isSelect; /**< 判断第几次点击 */
@property (nonatomic, assign) BOOL isUp; /**< 判断是否点赞 */

@property (nonatomic, retain) NSIndexPath *index;  /**< 记录下标 */
@end
@implementation LYMusicTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
        
        [self creatCollectionView];
        
        _isSelect = YES;
        _isUp = YES;
    }
    return self;
}

// 赋值
- (void)setModel:(LYMusicModel *)model {
    _model = model;
    
    [_imgView sd_setImageWithURL:[NSURL URLWithString:model.photo]];
    [_smallImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    
    _autherLabel.text = model.uname;
    
    if (model.isSelected == NO) {
        
        _clickImage.image = [UIImage imageNamed:@"iconfont-yinlebofang"];
        // 给clickImage添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_clickImage addGestureRecognizer:tap];
        
        
    }else {
        _clickImage.image = [UIImage imageNamed:@"iconfont-zanting"];
        // 给clickImage添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_clickImage addGestureRecognizer:tap];
        
    }
    
    _clickImage.userInteractionEnabled = YES;
    
    [_titleButton setTitle:model.name forState:UIControlStateNormal];
    [_titleButton setTitleColor:[UIColor colorWithRed:0.13 green:0.7 blue:0.99 alpha:1] forState:UIControlStateNormal];
    [_titleButton addTarget:self action:@selector(titleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_singerButton setTitle:model.singer forState:UIControlStateNormal];
    [_singerButton setTitleColor:[UIColor colorWithRed:0.13 green:0.7 blue:0.99 alpha:1] forState:UIControlStateNormal];
    
    // 判断进度条有无
    if (_model.isHidden) {
        
        [_timeSlider setHidden:NO];
        
    }else {
        
        [_timeSlider setHidden:YES];
        
    }
    
    
//    _num = _model.liked_num;
    
    [_bottonmCollectionView reloadData];
}



// 创建视图
- (void)createView {
    
    self.imgView = [[UIImageView alloc] init];
    self.imgView.userInteractionEnabled = YES;
    [self.contentView addSubview:_imgView];
    
    self.smallImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_smallImageView];
    
    self.autherLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_autherLabel];
    

    
    self.clickImage = [[UIImageView alloc] init];
    [self.imgView addSubview:_clickImage];
    
    
    self.titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_titleButton];
    
    self.singerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_singerButton];
    
    self.timeSlider = [[UISlider alloc] init];
    
    [self.contentView addSubview:_timeSlider];
    [_timeSlider setThumbImage:[UIImage imageNamed:@"iconfont-liangkehongxin"] forState:UIControlStateNormal];
    
    [_timeSlider setHidden:YES];
    
    
}

// 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    
    _imgView.frame = CGRectMake(5 * wt, 60 * ht, self.contentView.frame.size.width - 10 * wt, 280 * ht);
    
    _smallImageView.frame = CGRectMake(10 * wt, 10 * ht, 40 * wt, 40 * wt);
    _smallImageView.layer.cornerRadius = 20 * wt;
    _smallImageView.layer.masksToBounds = YES;
    
    
    _autherLabel.frame = CGRectMake(60 * wt, 10 * ht, self.contentView.frame.size.width, 40 * ht);
    
    
    _clickImage.frame = CGRectMake(_imgView.frame.size.width - 60 * wt, _imgView.frame.size.height - 60 * wt, 50 * wt, 50 * wt);

    
    _titleButton.frame = CGRectMake(10 * wt, _imgView.frame.size.height - 90 * wt, self.contentView.frame.size.width - 20 * wt, 40 * ht);
    
    
    
}


- (void)setIndexPath:(NSIndexPath *)indexPath {
   
    _index = indexPath;
    
}
#pragma mark - button 点击方法
- (void)titleButtonAction:(UIButton *)button {
    
    [self.delegate jumpNextPage:_index];
}

/** 播放图片的轻怕方法 */
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    [_timeSlider setHidden:NO];
    
    
    if (_isSelect) {
        
    // 点击播放后创建进度条
    _timeSlider.frame = CGRectMake(5 * wt, _imgView.frame.size.height + _imgView.frame.origin.y - 15 * ht, _imgView.frame.size.width, 30 * ht);
    [_timeSlider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];
        
        
        _clickImage.image = [UIImage imageNamed:@"iconfont-zanting"];
        
        
        LY_sharePlayeMusicHelp.playToEndBlock = ^(){
            NSLog(@"播放完我单例的观察者会观察到 就会调用block 走这个方法, 播放完在这里做你想做的");
            
            _clickImage.image = [UIImage imageNamed:@"iconfont-yinlebofang"];
            [_timeSlider setHidden:YES];
        };
        
        //   // 0.1秒回调
        LY_sharePlayeMusicHelp.playTimerBlock = ^(CGFloat timer){
            
          [self playTimer:timer]; //调用方法
            
        };

        //播放
        [LY_sharePlayeMusicHelp playMusicMp3Url:_model.stream];
        
        _model.isSelected = YES;
        _model.isHidden = YES;
        
    }else {
        [_timeSlider setHidden:YES];
        _clickImage.image = [UIImage imageNamed:@"iconfont-yinlebofang"];
        [LY_sharePlayeMusicHelp pause];
        _model.isSelected = NO;
        _model.isHidden = NO;
        
    }
    _isSelect = !_isSelect;
}


// 进度条滑动的方法
- (void)slider:(UISlider *)slider {
    
    [LY_sharePlayeMusicHelp seekToTime:slider.value];
    
}

// 给进度条赋值
- (void)playTimer:(CGFloat)timer {
    
    self.timeSlider.value = timer / LY_sharePlayeMusicHelp.sumtime;
}

// 创建collectionView
- (void)creatCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 50)/ 4, 40 * ht);
    self.bottonmCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 400 * ht - 60 * ht, [UIScreen mainScreen].bounds.size.width, 60 * ht) collectionViewLayout:layout];
    [self.contentView addSubview:_bottonmCollectionView];
    
    _bottonmCollectionView.delegate = self;
    _bottonmCollectionView.dataSource = self;
    
    _bottonmCollectionView.backgroundColor = [UIColor clearColor];
    [_bottonmCollectionView registerClass:[LYMusicCollectionViewCell class] forCellWithReuseIdentifier:@"bottonmCollectionView"];
    
}


#pragma mark - delegate & datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LYMusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bottonmCollectionView" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:0.2];
    if (indexPath.row == 0) {
        if (_model.isSelected2 == NO) {
            
//            cell.firstImage.image = [UIImage imageNamed:@"iconfont-dianzan.png"];
            
            cell.firstLabel.text = [_model.liked_num stringValue];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            
            if ([defaults objectForKey:@"row"] != NULL) {
                
                if (_index.section == [defaults integerForKey:@"row"]) {
                    
                    cell.firstImage.image = [UIImage imageNamed:@"iconfont-dianzan-2"];
                    NSInteger count = [_model.liked_num intValue] + 1;
                    cell.firstLabel.text = [NSString stringWithFormat:@"%ld", count];
                    
                }else {
                    cell.firstImage.image = [UIImage imageNamed:@"iconfont-dianzan.png"];

                }
                
            }
        }else {
            cell.firstImage.image = [UIImage imageNamed:@"iconfont-dianzan-2"];
            NSInteger count = [_model.liked_num intValue] + 1;
            cell.firstLabel.text = [NSString stringWithFormat:@"%ld", count];
            
        }
        
        
    }else if (indexPath.row == 1) {
        cell.firstImage.image = [UIImage imageNamed:@"iconfont-groupcopy5.png"];
        cell.firstLabel.text = @"评论";
    }else if (indexPath.row == 2) {
        cell.firstImage.image = [UIImage imageNamed:@"iconfont-shoucang-2.png"];
        cell.firstLabel.text = @"收藏";
    }else {
        cell.firstImage.image = [UIImage imageNamed:@"iconfont-jinlingyingcaiwangtubiao41.png"];
        cell.firstLabel.text = @"更多";
    }
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    LYMusicCollectionViewCell *cell = (LYMusicCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
   
    if (indexPath.row == 0) {
        
        if (_isUp) {
            
            cell.firstImage.image = [UIImage imageNamed:@"iconfont-dianzan-2"];
            NSInteger count = [_model.liked_num intValue] + 1;
            cell.firstLabel.text = [NSString stringWithFormat:@"%ld", count];
            
            [[NSUserDefaults standardUserDefaults] setInteger:_index.section forKey:@"row"];
            
            _model.isSelected2 = YES;
            
        }else {
            cell.firstImage.image = [UIImage imageNamed:@"iconfont-dianzan.png"];
            cell.firstLabel.text = [_model.liked_num stringValue];
            _model.isSelected2 = NO;
            
        }
        _isUp = !_isUp;
        
        [_bottonmCollectionView reloadData];
        
    }else if (indexPath.row == 3) {
        
    }
    
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
